import {
    ICreateClubReq,
    ICreateClubRes,
    IDeleteClubReq,
    IGetAllClubApplicationReq,
    IGetAllClubApplicationRes,
    IGetAllClubFormReq,
    IGetAllClubFormRes,
    IGetAllClubsReq,
    IGetAllClubsRes,
    IGetClubPageReq,
    IGetClubReq,
    ISearchClubReq,
    ISearchClubRes,
    ISearchUserRes,
    IUpdateClubReq,
    IUpdateClubRes,
    IUpsertClubPageReq,
    ReqApiUrls
} from "@/headers/admin/reqs.h";
import { IAdminStore } from "@/headers/admin/store.h";
import {apiAxios as axios} from "@/utils/axios";
import { Store, ActionContext } from "vuex";
import { ActionBase, registerAll } from "@/store/admin/common";
import {omit} from 'lodash-es';
import {IClubModel, IClubPageModel} from "@/headers/admin/model.h";
import { pick } from "vant/lib/utils";


// club

export class GetAllClubsAction extends ActionBase<IAdminStore,IAdminStore,IGetAllClubsReq,IGetAllClubsRes> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: IGetAllClubsReq): Promise<IGetAllClubsRes> {
        const {data} = await axios.get<IGetAllClubsRes>(ReqApiUrls.Clubs,{params:payload})
        return data
    }
}

export class CreateClubAction extends ActionBase<IAdminStore,IAdminStore,ICreateClubReq,ICreateClubRes> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: ICreateClubReq): Promise<ICreateClubRes> {
        const {data} = await axios.post<ICreateClubRes>(ReqApiUrls.Clubs,payload);
        return data;
    }
}

export class UpdateClubAction extends ActionBase<IAdminStore,IAdminStore,IUpdateClubReq,IUpdateClubRes> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: IUpdateClubReq): Promise<IUpdateClubRes> {
        const {data} = await axios.put<IUpdateClubRes>(ReqApiUrls.Club,omit(payload,"id"),{
            templateParam:{
                clubID:payload.club_id
            }
        })
        return data;
    }

}

export class DeleteClubAction extends ActionBase<IAdminStore,IAdminStore,IDeleteClubReq,void> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: IDeleteClubReq): Promise<void> {
        const {data} = await axios.delete<void>(ReqApiUrls.Club,{templateParam:{clubId:payload.club_id}})
        return data;
    }

}

export class GetClubPageAction extends ActionBase<IAdminStore,IAdminStore,IGetClubPageReq,IClubPageModel> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: IGetClubPageReq): Promise<IClubPageModel> {
        const {data} = await axios.get<IClubPageModel>(ReqApiUrls.ClubPage,{templateParam:{clubID:payload.club_id}});
        return data;
    }

}

export class UpsertClubPageAction extends ActionBase<IAdminStore,IAdminStore,IUpsertClubPageReq,IClubPageModel> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: IUpsertClubPageReq): Promise<IClubPageModel> {
        const {data} = await axios.post<IClubPageModel>(ReqApiUrls.ClubPage,omit(payload,"club_id"),{
            templateParam:{clubID:payload.club_id}
        })
        return data;
    }
}

export class GetAllFormAction extends ActionBase<IAdminStore,IAdminStore,IGetAllClubFormReq,IGetAllClubFormRes> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: IGetAllClubFormReq): Promise<IGetAllClubFormRes> {
        const {data} = await axios.get<IGetAllClubFormRes>(ReqApiUrls.ClubForms,{params:omit(payload,"club_id"),templateParam:{clubID:payload.club_id}})
        return data;
    }
}

export class GetAllClubApplicationAction extends ActionBase<IAdminStore,IAdminStore,IGetAllClubApplicationReq,IGetAllClubApplicationRes> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: IGetAllClubApplicationReq): Promise<IGetAllClubApplicationRes> {
        const {data} = await axios.get<IGetAllClubApplicationRes>(ReqApiUrls.ClubApplications,{params:omit(payload,"club_id"),templateParam:{clubID:payload.club_id}})
        return data;
    }
}

export class SearchClubAction extends ActionBase<IAdminStore,IAdminStore,ISearchClubReq,ISearchClubRes> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: ISearchClubReq): Promise<ISearchClubRes> {
        const {data} = await axios.post<ISearchClubRes>(ReqApiUrls.ClubSearch,pick(payload,['where']),{
            params:omit(payload,'where')
        });
        return data;
    }
}

export class GetClubAction extends ActionBase<IAdminStore,IAdminStore,IGetClubReq,IClubModel> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: IGetClubReq): Promise<IClubModel> {
        const {data} = await axios.get<IClubModel>(ReqApiUrls.Club,{templateParam:{
            clubID:payload.club_id
        }})
        return data;
    }
}

export function initAdminClubAction(root:any) {
    registerAll(
        root,
        GetAllClubsAction,
        CreateClubAction,
        UpdateClubAction,
        GetClubPageAction,
        UpsertClubPageAction,
        GetAllFormAction,
        GetAllClubApplicationAction,
        SearchClubAction,
        GetClubAction
    )
}
